<template>
  <div>
    <!--输入框-->
    <div>
      <!--文本输入框-->
      <el-input v-model="jl.name" placeholder="添加职称等级..." prefix-icon="el-icon-plus" style="width: 300px" size="small"> </el-input>
      <!--下拉框-->
      <el-select size="small" v-model="jl.titleLevel" placeholder="职称登录" style="margin-left: 6px;margin-right: 6px">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>

      <!--添加按钮-->
      <el-button @click="addJobLevel" size="small" type="primary" icon="el-icon-plus">添加</el-button>
    </div>

    <!--表格-->
    <div style="margin-top: 10px">
      <el-table
          :data="jls"
          stripe
          border
          size="small"
          @selection-change="handleSelectionChange"
          style="width: 70%">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            width="150"
            label="职称等级">
        </el-table-column>
        <el-table-column
            prop="createDate"
            width="150"
            label="创建日期">
        </el-table-column>
        <el-table-column
            prop="enabled"
            width="100"
            label="是否启用">
          <template v-slot:default="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <template #default="scope">
            <el-button @click="showEditView(scope.row)" size="small">编辑</el-button>
            <el-button @click="onDelete(scope.row)" size="small" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--批量删除功能按钮-->
    <el-button @click="batchDelete" :disabled="multipleSelection.length === 0" size="small" style="margin-top: 8px" type="danger">批量删除</el-button>

    <!--对话框-->
    <el-dialog
        title="编辑职称"
        :visible.sync="dialogVisible"
        width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职称名称</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.name" size="small" style="margin-left: 6px"></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职称等级</el-tag>
          </td>
          <td>
            <!--下拉框-->
            <el-select size="small" v-model="updateJl.titleLevel" placeholder="职称登录" style="margin-left: 6px;margin-right: 6px">
              <el-option
                  v-for="item in titleLevels"
                  :key="item"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch
                style="margin-left: 6px"
                v-model="updateJl.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已启用"
                inactive-text="未启用">
            </el-switch>
          </td>
        </tr>
      </table>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="onUpdate">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "JobLevelMana",
  data(){
    return {
      jl: {
        name: '',
        titleLevel: ''
      },
      titleLevels: [
          '正高级',
          '副高级',
          '中级',
          '初级',
          '员级',
      ],
      jls: [],
      dialogVisible: false,
      updateJl: {
        name: '',
        titleLevel: '',
        enabled: false
      },
      multipleSelection: []
    }
  },
  methods: {
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    //批量删除
    batchDelete(){
      this.$confirm('此操作将永久删除：'+ this.multipleSelection.length +'条职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids ='?'
        this.multipleSelection.forEach(item => ids += 'ids=' + item.id + '&')
        this.deleteRequest('/system/basic/joblevel/' + ids).then(res => {
          if (res){
            //批量删除后刷新数据
            this.initJls()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    //数据初始化
    initJls(){
      this.getRequest('/system/basic/joblevel/').then(res => {
        if (res){
          this.jls = res
          //添加后清空数据
          this.jl.name = ''
          this.jl.titleLevel = ''
        }
      })
    },
    //添加
    addJobLevel(){
      if (this.jl.name && this.jl.titleLevel){
        this.postRequest('/system/basic/joblevel/', this.jl).then(res => {
          if (res){
            //刷新数据
            this.initJls()
          }
        })
      } else {
        this.$message.error('字段不能为空！')
      }
    },
    //删除
    onDelete(row){
      this.$confirm('此操作将永久删除：'+ row.name +', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/joblevel/' + row.id).then(res => {
          if (res){
            //删除后刷新数据
            this.initJls()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //显示对话框
    showEditView(row){
      //这里直接赋值会有数据绑定，修改输入框，表格数据也会修改，要改为复制属性
      //this.updateJl = row
      //复制属性解决上面的bug
      Object.assign(this.updateJl, row)
      //将日期值置为空，由后端来处理日期,否则修改会报未知错误
      this.updateJl.createDate = ''
      this.dialogVisible = true
    },
    //修改
    onUpdate(){
      this.putRequest('/system/basic/joblevel/', this.updateJl).then(res => {
        if (res){
          this.initJls()
          this.dialogVisible = false
        }
      })

    }

  },
  mounted() {
    this.initJls()
  }
}
</script>

<style scoped>

</style>
